package com.zsy.controller;

import com.zsy.pojo.R;
import com.zsy.pojo.ShoppingCart;
import com.zsy.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart, HttpSession session){
        Long userId = (Long) session.getAttribute("user");

        shoppingCart.setUserId(userId);

        return R.success(shoppingCartService.save(shoppingCart));
    }

    @GetMapping("list")
    public R<List<ShoppingCart>> list(HttpSession session){
        Long userId = (Long) session.getAttribute("user");

        return R.success(shoppingCartService.list(userId));
    }

    @DeleteMapping("/clean")
    private R<String> clean(HttpSession session){
        Long userId = (Long) session.getAttribute("user");

        shoppingCartService.clean(userId);
        return R.success("清空购物车成功");

    }

    @PostMapping("sub")
    public R<ShoppingCart> sud(@RequestBody ShoppingCart shoppingCart,HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        shoppingCart.setUserId(userId);
        return R.success(shoppingCartService.reducedDishes(shoppingCart));
    }
}
